<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\model\AdminModel;
use app\model\ReportModel;
use think\db\Query;
use think\facade\Env;

class Report extends BaseController
{
    public function query()
    {
        $params = $this->request->param();
        $adminId = $this->request->userInfo->adminId;
//        $adminModel = AdminModel::find($adminId);

        $reportModel = new ReportModel();

        $tableName = Env::get('DB_PREFIX').$reportModel->getName();


        if (!empty($params['content'])) {
            $content = trim($params['content']);
            empty($content) || $reportModel = $reportModel->where($tableName.'.content','like','%'.$content.'%');
        }

        if (!empty($params['username'])) {
            $username = trim($params['username']);
            empty($username) || $reportModel = $reportModel->hasWhere('user',function (Query $query) use($username){
                $query->where('username','like','%'.$username.'%');
            });
        }

        if (!empty($params['status'])) {
            $reportModel = $reportModel->where($tableName.'.status',$params['status']);
        }

        if (!empty($params['submit_time'])) {
            $s = strtotime(trim($params['submit_time'][0],'"'));
            $e = strtotime(trim($params['submit_time'][1],'"'));
            $reportModel = $reportModel->whereBetweenTime($tableName.'.submit_time',$s,$e);
        }

        $list = $reportModel
            ->where($tableName.'.admin_id',$adminId)
            ->with(['user' => function($query) {
                $query->withField(['id', 'username']);
                    },'img'])
            ->order([
//                'status' => 'asc',
                'submit_time' => 'desc'
            ])
            ->paginate([
                'list_rows' => $params['pageSize'],
                'page' => $params['pageNo']
            ]);


        return show_serve($list);

    }
    public function changeStatus()
    {
        $params = $this->request->param();

        $reportModel = ReportModel::find($params['id']);
        $reportModel->status = $params['status'];
        $reportModel->save();

        return show_serve();


    }

    public function del()
    {
        $params = $this->request->param();
        ReportModel::destroy($params['id']);
        return show_serve();
    }

}